package com.benjaminwan.read.free.utils

import android.app.Activity
import android.content.Context
import android.widget.Toast
import androidx.annotation.MainThread
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
import com.xuexiang.xui.widget.toast.XToast

/**
 * xtoast 工具类
 */

fun initXToast() {
    val config = XToast.Config.get()
        .setAlpha(200)
        .allowQueue(false)
}

@MainThread
fun Context.showXToastNormal(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    XToast.normal(this, message, duration).show()
}

@MainThread
fun Context.showXToastNormal(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
    XToast.normal(this, message, duration).show()
}


fun Activity.showXToastNormal(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    applicationContext.showXToastNormal(message, duration)
}

fun Activity.showXToastNormal(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
    applicationContext.showXToastNormal(message, duration)
}

fun Fragment.showXToastNormal(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    requireActivity().applicationContext.showXToastNormal(message, duration)
}

fun Fragment.showXToastNormal(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
    requireActivity().applicationContext.showXToastNormal(message, duration)
}

//=============//

@MainThread
fun Context.showXToastError(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    XToast.error(this, message, duration).show()
}

@MainThread
fun Context.showXToastError(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
    XToast.error(this, message, duration).show()
}

fun Activity.showXToastError(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    applicationContext.showXToastError(message, duration)
}

fun Activity.showXToastError(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
    applicationContext.showXToastError(message, duration)
}

fun Fragment.showXToastError(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    requireActivity().applicationContext.showXToastError(message, duration)
}

fun Fragment.showXToastError(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
    requireActivity().applicationContext.showXToastError(message, duration)
}

//=============//

@MainThread
fun Context.showXToastSuccess(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    XToast.success(this, message, duration).show()
}

@MainThread
fun Context.showXToastSuccess(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
    XToast.success(this, message, duration).show()
}

fun Activity.showXToastSuccess(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    applicationContext.showXToastSuccess(message, duration)
}

fun Activity.showXToastSuccess(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
    applicationContext.showXToastSuccess(message, duration)
}

fun Fragment.showXToastSuccess(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    requireActivity().applicationContext.showXToastSuccess(message, duration)
}

fun Fragment.showXToastSuccess(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
    requireActivity().applicationContext.showXToastSuccess(message, duration)
}
//=============//

@MainThread
fun Context.showXToastInfo(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    XToast.info(this, message, duration).show()
}

@MainThread
fun Context.showXToastInfo(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
    XToast.info(this, message, duration).show()
}

fun Activity.showXToastInfo(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    applicationContext.showXToastInfo(message, duration)
}

fun Activity.showXToastInfo(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
    applicationContext.showXToastInfo(message, duration)
}

fun Fragment.showXToastInfo(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    requireActivity().applicationContext.showXToastInfo(message, duration)
}

fun Fragment.showXToastInfo(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
    requireActivity().applicationContext.showXToastInfo(message, duration)
}
//=============//

@MainThread
fun Context.showXToastWarning(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    XToast.warning(this, message, duration).show()
}

@MainThread
fun Context.showXToastWarning(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
    XToast.warning(this, message, duration).show()
}

fun Activity.showXToastWarning(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    applicationContext.showXToastWarning(message, duration)
}

fun Activity.showXToastWarning(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
    applicationContext.showXToastWarning(message, duration)
}

fun Fragment.showXToastWarning(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    requireActivity().applicationContext.showXToastWarning(message, duration)
}

fun Fragment.showXToastWarning(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
    requireActivity().applicationContext.showXToastWarning(message, duration)
}
